# -*- coding: utf-8 -*-
# --------------------------------------
# @Time    : 2025/7/16 10:23
# @File    : sayhello.py
# @Project : P0716sayhello
# @Author  : CHH
# @Desc    : Windows   文件描述
# @Copyright : © 2025 HORDOR . All rights reserved.
# @Version : 1.0.0
# --------------------------------------
from flask import Blueprint, render_template, request, redirect
from utils.sqlhelper import db
from datetime import datetime

bd_bp = Blueprint("board", __name__)


@bd_bp.route("/index", methods=["GET", "POST"])
def index():
    if request.method == "GET":
        messages = db.fetch_all("select * from `messages` order by timestamp DESC",())
        return render_template("sayhello.html", messages=messages)
    name = request.form.get("name")
    textarea = request.form.get("textarea")
    timestamp = datetime.now()
    print(type(timestamp))
    db.insert_one("insert into messages (name,body,timestamp) values (%s,%s,%s)",
                  (name, textarea, timestamp))
    return redirect("/index")


@bd_bp.route("/error")
def error():
    return render_template("error.html")
